//
//  WMHomeViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/2.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMHomeViewController.h"
#import "WMHomeItemCell.h"

#import "WMPublicMsgViewController.h"
#import "WMFoodsViewController.h"
#import "WMMeetingViewController.h"
#import "WMParkingViewController.h"
@interface WMHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end
#define itemCell @"itemCell"
#define itemHeader @"itemHeader"

@implementation WMHomeViewController

{
    UICollectionView *collection;
    NSArray *classArray;
    NSArray *titleArray;
    NSArray *iconArray;
    NSArray <UIColor *>*colorArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"智慧晨光";
    self.view.backgroundColor = [UIColor whiteColor];
    
    classArray = @[@"WMPublicMsgViewController",
                   @"WMParkingViewController",
                   @"WMFoodsViewController",
                   @"WMMeetingViewController",
                   @"WMPublicMsgViewController",
                   @"WMPublicMsgViewController",
                   @"WMPublicMsgViewController"];
    
    iconArray = @[@"information.png",@"park.png",@"mess.png",@"meeting.png",@"building.png",@"marketing.png",@"more.png"];
    titleArray = @[@"信息公告",@"智慧停车",@"智慧食堂",@"智慧会议",@"智慧楼宇",@"智慧营销",@"更多"];
    colorArray = @[RGB(255, 199, 80, 1),
                   RGB(50, 165, 220, 1),
                   RGB(68, 195, 160, 1),
                   RGB(161, 135, 216, 1),
                   RGB(23, 158, 133, 1),
                   RGB(237, 126, 187, 1),
                   RGB(65,147, 284, 1)];
    [self createCollection];

    [self registCell];

}

-(void)registCell{
    
    [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:itemHeader];
    [collection registerNib:[UINib nibWithNibName:@"WMHomeItemCell" bundle:nil] forCellWithReuseIdentifier:itemCell];
}

-(void)createCollection{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //    [layout setHeaderReferenceSize:CGSizeMake(SCREEN_W, 200)];
    collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    collection.delegate = self;
    collection.dataSource = self;
    [self.view addSubview:collection];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 7;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_W/3-60, SCREEN_W/3-20);
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    UIImage *image = [UIImage imageNamed:@"gg1"];
    CGSize imageSize = image.size;
    return CGSizeMake(SCREEN_W, SCREEN_W/(imageSize.width/imageSize.height));
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:itemHeader forIndexPath:indexPath];
        SDCycleScrollView *cycle = [SDCycleScrollView cycleScrollViewWithFrame:view.frame imageNamesGroup:@[@"gg1",@"gg2",@"gg3",@"gg4",]];
        [view addSubview:cycle];
        return view;
    }
    
    
    return nil;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 30, 0, 30);
//    return UIEdgeInsetsZero;
}
//
//设置水平间距 (同一行的cell的左右间距）
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 30;
}
//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}



-  (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WMHomeItemCell *cell = [collection dequeueReusableCellWithReuseIdentifier:itemCell forIndexPath:indexPath];
    cell.backV.backgroundColor = colorArray[indexPath.item];
    cell.backV.layer.cornerRadius = (SCREEN_W/3-60)/2;
    cell.backV.layer.masksToBounds = YES;
    cell.icon.image = [UIImage imageNamed:iconArray[indexPath.item]];
    cell.title.text = titleArray[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.item>4){
        return;
    }
    
    if (indexPath.item == 4) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://221.226.124.194:8901"]];  //内网
        return;
    }
    
    UIViewController *controller = [[NSClassFromString(classArray[indexPath.row]) alloc]init];
    controller.hidesBottomBarWhenPushed = YES;
    controller.title = titleArray[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
